package com.orangelife.mobile.housepurchasing.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.PhoneCallUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.housepurchasing.adapter.ImageAdapter;
import com.orangelife.mobile.housepurchasing.biz.HouseDetailHttpBiz;
import com.orangelife.mobile.housepurchasing.biz.HouseListHttpBiz;
import com.orangelife.mobile.widget.ScrollGridView;
import com.orangelife.mobile.web.WebWin;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 房源展示的界面
 * Created by HeYuCan on 2016/5/17.
 */
public class HouseShowingActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.iv_title_back)
    ImageView ivBack;

    /**
     * 点击进入房源委托界面的textview
     */

    @Bind(R.id.tv_title_entrust)
    TextView entrust;
    /**
     * 社区概况
     */
    @Bind(R.id.tv_community_survey)
    TextView tvCommSurvey;

    @Bind(R.id.v_community_survey)
    View vCommSurvey;
    /**
     * 店长点评
     */
    @Bind(R.id.tv_shopowner_comment)
    TextView tvShopownerComment;

    @Bind(R.id.v_shopowner_comment)
    View vShopownerComment;
    /**
     * 房屋图片(封面图片)
     */
    @Bind(R.id.iv_house_pic)
    ImageView ivHousePhoto;
    /**
     * 房屋名称,如融侨水乡一期
     */
    @Bind(R.id.tv_house_name)
    TextView tvHouseNmae;

    @Bind(R.id.tv_content)
    TextView tvContent;
    /**
     * WebWin图片集
     */
    @Bind(R.id.wwPhoto)
    WebWin wwPhoto;

    @Bind(R.id.tv_reviews)
    TextView tvReviews;

    /*
     *小区图片
     */
    @Bind(R.id.lv_comm_image)
    ListView lvCommImage;
    /**
     * 滚动view
     */
    ScrollGridView gvWeight;

    /**
     * 图片下载
     */
    DisplayImageOptions imgOptions;
    ImageLoader loadImage = ImageLoader.getInstance();



    /**
     * 店主点评的显示视图
     */
    @Bind(R.id.ll_comment_content)
    LinearLayout llComentContent;
    /**
     * 社区概况的滚动视图
     */
    @Bind(R.id.ll_house_survey_scroll)
    LinearLayout llHouseSurveyScroll;
    /**
     * 类型
     */
    @Bind(R.id.tv_is_secondhand)
    TextView tvSecondhand;

    private ImageDownloader image = ImageDownloader.getImageDownloader();
    /*从list传来的房屋销售ID*/
    private String saleID = "";
    /*房屋详情概览*/
    private List<Map<String, Object>> mList;
    /*存放服务器返回的房屋详情map*/
    private Map<String, Object> mapFromServer = new HashMap<>();
    private String saleImgsURL;
    private String coverImgURL;
    private String commImgURL;
    String saleType;
    private String telephone;
    private ImageAdapter imageAdapter=null;
    private List<String> list=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_showing);
        ButterKnife.bind(this);
        requestHouseDetail();
        initView();

    }

    private void initView(){
        list=new ArrayList<String>();
        imageAdapter=new ImageAdapter(HouseShowingActivity.this,list);
        lvCommImage.setAdapter(imageAdapter);
    }



    private void requestHouseDetail() {
        Intent intent = getIntent();
       saleID = intent.getStringExtra("saleID");

        //     URL要加上saleID
//        Map<String,Object> map=new
        HouseDetailHttpBiz.requestHouseDetail(saleID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                mapFromServer = HouseDetailHttpBiz.getHouseDetailAsMap(response);
                mList = (List<Map<String, Object>>) mapFromServer.get("summary");
                StringBuffer sb=new StringBuffer();
                for (Map<String, Object> map : mList
                        ) {
                    String name = (String) map.get("name");
                    String value = (String) map.get("value");

                    sb.append(name+"："+value+"\n");
                }
                tvContent.setText(sb.toString());
                findView();
                loadImages();
            }
        });

    }

    /**
     * 下载图片
     */
    private void loadImages() {
        imgOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.shop_header)
                .showImageForEmptyUri(R.drawable.shop_header)
                .showImageOnFail(R.drawable.shop_header)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(1))
                .build();

        /*封面图片*/
        loadImage.displayImage(coverImgURL.trim(), ivHousePhoto, imgOptions);
        /*小区图片*/
        if(commImgURL.indexOf("##")>-1){
            String []images=commImgURL.split("##");
            for (int i=0;i<images.length;i++){
                list.add(images[i]);
            }
        }else{
            list.add(commImgURL);
        }

        imageAdapter.notifyDataSetChanged();
        /*相关图片*/
        wwPhoto.loadDataWithBaseURL(saleImgsURL.trim());
    }


    private void findView() {
        /*房屋销售类型,如服务器返回"1",对应"出售"*/
        HashMap<String,String> saleTypeMap=new HashMap<>();

         saleType=String.valueOf(mapFromServer.get("saleType"));

        telephone=String.valueOf(mapFromServer.get("hotline"));
        saleImgsURL = String.valueOf(mapFromServer.get("saleImgs"));
        coverImgURL = String.valueOf(mapFromServer.get("cover"));
        commImgURL = String.valueOf(mapFromServer.get("commImg"));
        tvHouseNmae.setText(String.valueOf(mapFromServer.get("commName")));
        tvSecondhand.setText(" "+HouseListHttpBiz.saleTypeCn(saleType)+ " ");
        tvReviews.setText(String.valueOf(mapFromServer.get("content")).equals("null")?"":String.valueOf(mapFromServer.get("content")));
     /*设置图片比例*/
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = width * 9 / 16;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        ivHousePhoto.setLayoutParams(params);




    }



    @OnClick({R.id.iv_title_back, R.id.tv_title_entrust,R.id.ll_seehouse,R.id.tv_community_survey,R.id.tv_shopowner_comment})
    public void onClick(View view){
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_title_back:
                    /*返回到房源列表界面*/
                finish();
                break;

            case R.id.tv_title_entrust:
                    /*点击进入房源委托界面*/
                intent = new Intent(HouseShowingActivity.this, ToEntrustActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_seehouse:
                PhoneCallUtil.getInstances().callPhone(HouseShowingActivity.this, telephone, "呼叫橙子置业房源委托热线?");
                break;
            case R.id.tv_community_survey:
                    /*社区概况*/
                llHouseSurveyScroll.setVisibility(View.GONE);
                llComentContent.setVisibility(View.VISIBLE);
                tvCommSurvey.setTextColor(getResources().getColor(R.color.lightOrange));
                tvShopownerComment.setTextColor(getResources().getColor(R.color.text_color_blank));
                vShopownerComment.setVisibility(View.INVISIBLE);
                vCommSurvey.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_shopowner_comment:
                  /*店长点评页面*/
                llHouseSurveyScroll.setVisibility(View.VISIBLE);
                llComentContent.setVisibility(View.GONE);
                tvShopownerComment.setTextColor(getResources().getColor(R.color.lightOrange));
                vShopownerComment.setVisibility(View.VISIBLE);
                tvCommSurvey.setTextColor(getResources().getColor(R.color.text_color_blank));
                vCommSurvey.setVisibility(View.INVISIBLE);
                break;
            default:
                break;
        }
    }

}
